<?php
require 'Client.php';

$server = stream_socket_server('tcp://0.0.0.0:9001', $errno, $errStr);

// 事件库
$eventBase = new EventBase();

$what = Event::PERSIST | Event::READ | EVENT::WRITE;

// 定义一个数组
$events = [];

// 事件
$event = new Event(
    $eventBase,
    $server,
    $what,
    function ($server, $what) use ($eventBase, &$events) {
        echo '事件开始' . PHP_EOL;

        $client = stream_socket_accept($server);

        (new Client($eventBase, $client, $what, $events))->handle();

         echo '事件结束' . PHP_EOL;
});

$event->add();

$events[(int) $server][$what] = $event;

// 执行事件
$eventBase->loop();